using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MosziNET.Web.Gallery.Template;
using System.Globalization;
using MosziNET.Web.Gallery.Providers;
using MosziNET.Web.Gallery.Properties;
using MosziNET.Web.Gallery.Controls.Admin;

namespace MosziNET.Web.Gallery.Controls
{
    /// <summary>
    /// The Gallery control shows the folder structure of the gallery
    /// </summary>
    public class GalleryControl : CompositeControl
    {
        #region / ViewMode enum /

        /// <summary>
        /// Defines the actions the gallery can perform
        /// </summary>
        public enum ViewModeEnum
        {
            ViewFolder,
            ViewImage,
            ViewControl
        }

        #endregion / ViewMode enum /

        #region / Private fields and constants /

        // Holds the folder id
        private string folderId = "0";
        // the name of the image to show
        private string imageName = String.Empty;
        // the key of the custom control's viewstate member
        private const string keyCustomControl = "CustomControl";

        // repeats all the folders in the current folder
        Repeater galleryRepeater;
        // repeats all the images in the current folder
        Repeater imageRepeater;
        // the view mode of the control
        ViewModeEnum viewMode = ViewModeEnum.ViewFolder;

        Table folderView;
        ImageViewControl imageView;

        #endregion / Private fields and constants /

        #region / Public properties /

        /// <summary>
        /// Gets/sets the custom control's type
        /// </summary>
        public Type CustomControlType
        {
            get
            {
                if (ViewState[keyCustomControl] == null ||
                    ViewState[keyCustomControl] as Type == null)
                    return null;

                return (Type)ViewState[keyCustomControl];
            }
            set
            {
                ViewState[keyCustomControl] = value;
            }
        }

        /// <summary>
        /// Gets/sets the id of the folder to be displayed
        /// </summary>
        public string FolderId
        {
            get
            {
                return this.folderId;
            }
            set
            {
                if (value == null)
                    this.folderId = "0";
                else
                    this.folderId = value;
            }
        }

        /// <summary>
        /// Gets/sets the name of the image
        /// </summary>
        public string ImageName
        {
            get
            {
                return this.imageName;
            }
            set
            {
                this.imageName = value;
            }
        }

        /// <summary>
        /// Gets/sets the view mode of the control
        /// </summary>
        public ViewModeEnum ViewMode
        {
            get
            {
                return this.viewMode;
            }
            set
            {
                this.viewMode = value;
            }
        }

        #endregion / Public properties /

        #region / Overrides /

        /// <summary>
        /// Creates the controls of this Gallery
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (GalleryManager.IsUserAdmin)
            {
                this.Controls.Add(CreateAdminControls());
            }

            folderView = CreateFolderView();
            folderView.Visible = false;
            imageView = CreateImageView();
            imageView.Visible = false;
            
            this.Controls.Add(folderView);
            this.Controls.Add(imageView);
        }

        /// <summary>
        /// Creates additional controls that will help on administering the gallery
        /// </summary>
        private Control CreateAdminControls()
        {
            Panel adminPanel = new Panel();
            adminPanel.CssClass = GalleryManager.GetCssClass("AdminPanel");

            // build the link button
            LinkButton createFolder = new LinkButton();
            createFolder.Text = Resources.Text_CreateFolder;
            createFolder.Click += new EventHandler(createFolder_Click);

            // add the controls to the control collection
            adminPanel.Controls.Add(createFolder);

            return adminPanel;
        }

        /// <summary>
        /// Called before the rendering is started
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            switch (ViewMode)
            {
                // in case we have a folder view ...
                case ViewModeEnum.ViewFolder:
                    
                    galleryRepeater.DataSource = GalleryManager.StorageProvider.GetFolderList(this.FolderId);
                    imageRepeater.DataSource = GalleryManager.StorageProvider.GetImageList(this.FolderId);
                    galleryRepeater.DataBind();
                    imageRepeater.DataBind();

                    folderView.Visible = true;
                    break;

                // in case we have an image view
                case ViewModeEnum.ViewImage:
                    imageView.FolderImage = new FolderImage(this.FolderId, this.ImageName);
                    imageView.Visible = true;
                    break;

                // in case we have a custom control view 
                case ViewModeEnum.ViewControl:
                    
                    break;
            }
        }

        #endregion / Overrides /

        #region / Content builder methods /

        /// <summary>
        /// Creates the image view control
        /// </summary>
        /// <returns></returns>
        private ImageViewControl CreateImageView()
        {
            ImageViewControl imageView = new ImageViewControl();

            return imageView;
        }

        /// <summary>
        /// Creates the folder view control
        /// </summary>
        /// <returns></returns>
        private Table CreateFolderView()
        {
            // Create a surrounding table
            Table mainTable = new Table();
            TableRow oneRow = new TableRow();
            TableCell oneCell = new TableCell();
            mainTable.Controls.Add(oneRow);
            oneRow.Controls.Add(oneCell);

            if (!String.IsNullOrEmpty(GalleryManager.MainTableClass))
                mainTable.CssClass = GalleryManager.MainTableClass;

            if (!String.IsNullOrEmpty(GalleryManager.MainCellClass))
                oneCell.CssClass = GalleryManager.MainCellClass;

            // now create the gallery repeater
            galleryRepeater = new Repeater();
            galleryRepeater.ItemTemplate = new FolderTemplate(GalleryManager.CategoryThumbClass);
            galleryRepeater.ItemDataBound += new RepeaterItemEventHandler(galleryRepeater_ItemDataBound);

            // as a last add the two repeaters
            oneCell.Controls.Add(galleryRepeater);

            // now create the gallery repeater
            if (!String.IsNullOrEmpty(this.folderId))
            {
                imageRepeater = new Repeater();
                imageRepeater.ItemTemplate = new ImageTemplate(GalleryManager.ImageThumbClass);
                imageRepeater.ItemDataBound += new RepeaterItemEventHandler(imageRepeater_ItemDataBound);

                oneCell.Controls.Add(imageRepeater);
            }

            return mainTable;
        }

        #endregion / Content builder methods /

        #region / Events /

        /// <summary>
        /// Called when an item is data bound
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void galleryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.Controls.Count == 0)
                return;

            Folder boundFolder = e.Item.DataItem as Folder;
            FolderControl uiFolder = e.Item.Controls[0] as FolderControl;

            if (boundFolder != null && uiFolder != null)
            {
                uiFolder.folderLink.HRef = GalleryManager.GetFolderUrl(boundFolder.Id);
                uiFolder.folderLink.Title = boundFolder.Title;
                uiFolder.title.Text = boundFolder.Title;

                uiFolder.folderThumbnail.Src = HelperMethods.GetImageUrl(
                    boundFolder.Thumbnail, boundFolder.Id, true, true);
            }
        }

        /// <summary>
        /// Called when an item is data bound
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void imageRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.Controls.Count == 0)
                return;

            FolderImage boundImage = e.Item.DataItem as FolderImage;
            ImageControl uiImage = e.Item.Controls[0] as ImageControl;

            if (boundImage != null && uiImage != null)
            {
                uiImage.imageLink.HRef = GalleryManager.GetImageLinkUrl(boundImage.ParentFolder,
                    boundImage.ImageName);
                uiImage.imageThumbnail.Src = HelperMethods.GetImageUrl(
                    boundImage, String.Empty, false, true);
            }
        }

        /// <summary>
        /// Called when the create folder button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void createFolder_Click(object sender, EventArgs e)
        {
            this.ViewMode = ViewModeEnum.ViewControl;
            this.CustomControlType = typeof(CreateFolder);
        }

        #endregion / Events /
    }
}
